package com.gmzg.gmzgnews.news.fragments;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.gmzg.gmzgnews.R;
import com.gmzg.gmzgnews.news.adapters.HeadlineAdapter;
import com.gmzg.gmzgnews.news.entries.HeadlineResult;
import com.gmzg.gmzgnews.news.entries.NewsEntry;
import com.gmzg.gmzgnews.news.interfaces.NeteaseServer;
import com.jash.networklibrary.NetworkTask;
import com.jash.networklibrary.Tools;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * A simple {@link Fragment} subclass.
 */
public class SelecteFragment extends BaseFragment implements NetworkTask.Callback<HeadlineResult>{
    private HeadlineAdapter adapter;
    private NeteaseServer server;

    @BindView(R.id.news_selecte_listview)
    ListView mNewsSelecteListview;
    private ArrayList<NewsEntry> mNewsEntries;

    public SelecteFragment() {
        // Required empty public constructor
    }

    @Override
    public String getFragmentTitle() {
        return "精选";
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_news_selecte, container, false);
        ButterKnife.bind(this, view);
        mNewsEntries = new ArrayList<>();
        adapter = new HeadlineAdapter(getContext(), mNewsEntries);
        mNewsSelecteListview.setAdapter(adapter);
        server = Tools.getInstance(NeteaseServer.class);
        server.getHeadline(0, 20).execute(this);

        mNewsSelecteListview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(getContext(), mNewsEntries.get(position).getTitle()+"被点击了", Toast.LENGTH_SHORT).show();
            }
        });
        return view;
    }

    @Override
    public void onSuccess(HeadlineResult text) {
        adapter.addAll(text.getData());
    }

    @Override
    public void onFail(Exception e) {

    }
}
